package com.secneo.appshare.activities;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.secneo.appshare.AppShareApplication;
import com.secneo.appshare.R;
import com.secneo.appshare.adapter.RemindAdapter;
import com.secneo.appshare.common.Constants;
import com.secneo.appshare.threads.RemindThread;

public class RemindActivity extends Activity{

	Button mRetIv = null ;
	ListView mRemindLv = null ;
	RemindThread mRemindThread = null ;
	ProgressBar mWaitProgressBar = null ;
	TextView mEmptyListPromptTv = null ;
	RemindAdapter adapter = null;
	
	private int mActionType;
	private String mUserId;
	private String mUserName;
	private String mAvatarName;
	
	Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
			case Constants.GET_REMIND_ERR:
				mWaitProgressBar.setVisibility(View.GONE) ;
				View view = RemindActivity.this.findViewById(R.id.error_user);
				view.setVisibility(View.VISIBLE);
				break ;
			case Constants.GET_REMIND_FINISH:
				mWaitProgressBar.setVisibility(View.GONE) ;
				loadList() ;
				break ;
			case Constants.GET_REMIND_ICON_FINISH:
				mRemindLv.invalidateViews();
				break ;
			case Constants.GET_REMIND_NO_DATA:
				mWaitProgressBar.setVisibility(View.GONE) ;
				mEmptyListPromptTv.setVisibility(View.VISIBLE) ;
				break ;
			default:
				break;
			}
		}
	} ;
	
	@Override
	protected void onStop() 
	{
		if(mRemindThread!=null && mRemindThread.isAlive())
			mRemindThread.stopRun();
		super.onStop();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.appshare_remind) ;
		AppShareApplication.getInstance().addActivity(this);
		mRetIv = (Button)findViewById(R.id.img_ret) ;
		mRetIv.setOnClickListener(mRetListener) ;
		
		mRemindLv = (ListView)findViewById(R.id.private_msg_list) ;
		mRemindLv.setDivider(getResources().getDrawable(R.drawable.list_divider));
		mRemindLv.setVisibility(View.GONE) ;
		mWaitProgressBar = (ProgressBar)findViewById(R.id.wait_progress) ;
		

		Bundle bundle = getIntent().getExtras();
		mActionType = bundle.getInt("action_type", Constants.actionTypeFriend);
		mUserId = "";
		if(bundle.containsKey("userid")){
			mUserId = bundle.getString("userid");
		}
		mUserName = "";
		if(bundle.containsKey("username")){
			mUserName = bundle.getString("username");
		}
		mAvatarName = "";
		if(bundle.containsKey("avatarname")){
			mAvatarName = bundle.getString("avatarname");
		}
		
		mRemindThread = new RemindThread(mHandler, this, mActionType);
		mEmptyListPromptTv = (TextView)findViewById(R.id.empty_list_prompt) ;
		mRemindThread.start() ;
	}
	
	private void loadList(){
		mRemindLv.setVisibility(View.VISIBLE) ;
		adapter = new RemindAdapter(this , mRemindThread.getList()) ;
		mRemindLv.setAdapter(adapter) ;
	}
	
	private final OnClickListener mRetListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			finish() ;
		}
	} ;

	@Override
	protected void onDestroy()
	{
		mRemindLv.setAdapter(null);
		if(adapter != null)
		{
			adapter.freeDrawable();
			adapter = null;
		}
		super.onDestroy();
	}
}
